package com.quanyan.place.service.impl;

import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.Page;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.utils.DateUtils;
import com.quanyan.place.entity.TbBizPlaceEmployee;
import com.quanyan.place.entity.TbBizPlaceEmployeeExample;
import com.quanyan.place.entity.TbCity;
import com.quanyan.place.entity.apireq.ReqEmployeeInfo;
import com.quanyan.place.entity.apiresp.RespCommerceEmployee;
import com.quanyan.place.entity.vo.EmployeeFormDataVo;
import com.quanyan.place.entity.vo.VisitedFromDataVo;
import com.quanyan.place.mapper.TbBizPlaceEmployeeMapper;
import com.quanyan.place.mapper.TbBizPlaceTargetMapper;
import com.quanyan.place.mapper.TbCityMapper;
import com.quanyan.place.service.EmployeeService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by liuyaoxin on 2016/6/16
 */
@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService {


    private static final Logger logger = LoggerFactory.getLogger(EmployeeServiceImpl.class);

    @Autowired
    TbBizPlaceEmployeeMapper tbBizPlaceEmployeeMapper;

    @Autowired
    private TbCityMapper cityMapper;

    @Autowired
    TbBizPlaceTargetMapper tbBizPlaceTargetMapper;


    @Override
    public APIResponse importEmployeeData(TbBizPlaceEmployee vo) {
        tbBizPlaceEmployeeMapper.insert(vo);
        return APIResponse.returnSuccess();
    }

    @Override
    public  TbBizPlaceEmployee getEmployeeByUid(Integer uid) {
        TbBizPlaceEmployeeExample example = new TbBizPlaceEmployeeExample();
        TbBizPlaceEmployeeExample.Criteria tbBizPlaceEmployeeCriteria = example.createCriteria();
        tbBizPlaceEmployeeCriteria.andUidEqualTo(uid);
        List<TbBizPlaceEmployee> employee = tbBizPlaceEmployeeMapper.selectByExample(example);
        if (null != employee && employee.size()>0){
            return employee.get(0);
        }else{
            return  null;
        }

    }

    @Override
    public PageObj<List<TbBizPlaceEmployee>> getEmployeeList(ReqEmployeeInfo reqEmployeeInfo) {
        TbBizPlaceEmployeeExample example = new TbBizPlaceEmployeeExample();
        TbBizPlaceEmployeeExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(reqEmployeeInfo.getId());//查询出下一级的成员
        int total = tbBizPlaceEmployeeMapper.countByExample(example);
        Integer pageNum = 0;
        Integer pageSize = Constants.DEFAULT_PAGE_SIZE;
        pageNum = reqEmployeeInfo.getPageNum();
        pageSize = reqEmployeeInfo.getPageSize();
        example.setPage(new Page(pageNum * pageSize, pageSize, total));
        List<TbBizPlaceEmployee> list = tbBizPlaceEmployeeMapper.selectByExample(example);
        PageObj<List<TbBizPlaceEmployee>> result = PageObj.create(total, pageNum, pageSize, list);
        return result;
    }

    /**
     * 获取商务员工列表
     * @param reqEmployeeSearch
     * @return
     */
    @Override
    public PageObj<List<RespCommerceEmployee>> getEmployeeInfoList(ReqEmployeeInfo reqEmployeeSearch) {
        TbBizPlaceEmployeeExample example=new TbBizPlaceEmployeeExample();
        TbBizPlaceEmployeeExample.Criteria criterial = example.createCriteria();
        Integer pageNum = 0;
        Integer pageSize = Constants.DEFAULT_PAGE_SIZE;
        if (reqEmployeeSearch != null){
            if (reqEmployeeSearch.getCityId() != null){
                criterial.andCityIdEqualTo(reqEmployeeSearch.getCityId());
            }
            pageNum = reqEmployeeSearch.getPageNum();
            pageSize = reqEmployeeSearch.getPageSize();
            String keywords = reqEmployeeSearch.getName();
            if (StringUtils.isNotEmpty(keywords)) {
                criterial.andNameLike("%" + keywords + "%");
            }
        }
        List<RespCommerceEmployee> list = new ArrayList<RespCommerceEmployee>();
        int totalNum = tbBizPlaceEmployeeMapper.countByExample(example);
        example.setPage(new Page(pageNum * pageSize, pageSize, totalNum));
        List<TbBizPlaceEmployee> employeeList = tbBizPlaceEmployeeMapper.selectByExample(example);
        for (TbBizPlaceEmployee employee : employeeList){
            RespCommerceEmployee respEmployee = new RespCommerceEmployee();
            respEmployee.setId(employee.getId());
            respEmployee.setCityId(employee.getCityId());
            respEmployee.setUid(employee.getUid());
            respEmployee.setParentId(employee.getParentId());
            respEmployee.setTrueName(employee.getName());
            respEmployee.setTitle(employee.getTitle());
            if (null != employee.getEntryTime()){
                respEmployee.setEntryTime(DateUtils.convertDateFormatToString(employee.getEntryTime()));
            }
            if (null != employee.getParentId()){
                TbBizPlaceEmployee parentEmployee = tbBizPlaceEmployeeMapper.selectByPrimaryKey(employee.getParentId());
                respEmployee.setParentName(parentEmployee.getName());
            }
            if (null != employee.getCityId()){
                TbCity city = cityMapper.selectByPrimaryKey(employee.getCityId());
                respEmployee.setCityName(city.getName());
            }
            list.add(respEmployee);
        }
        PageObj<List<RespCommerceEmployee>> result = PageObj.create(totalNum, pageNum, pageSize, list);
        return result;
    }

    /**
     * 获取商务员工数量
     * @param cityId
     * @param keyWords
     * @return
     */
    public Integer countEmployee(Integer cityId,
                  String keyWords){
        TbBizPlaceEmployeeExample example=new TbBizPlaceEmployeeExample();
        TbBizPlaceEmployeeExample.Criteria criterial = example.createCriteria();
        if (cityId != null){
            criterial.andCityIdEqualTo(cityId);
        }
        if (StringUtils.isNotEmpty(keyWords)) {
            criterial.andNameLike("%" + keyWords + "%");
        }
        int totalNum = tbBizPlaceEmployeeMapper.countByExample(example);
        return totalNum;

    }

    /**
     * 获取商务员工列表
     * @param cityId
     * @param keyWords
     * @param pageNum
     * @param pageSize
     * @param totalNum
     * @return
     */
    public List<TbBizPlaceEmployee> getEmployeeList(Integer cityId,
                                                    String keyWords,
                                                    Integer pageNum,
                                                    Integer pageSize,
                                                    Integer totalNum){
        TbBizPlaceEmployeeExample example=new TbBizPlaceEmployeeExample();
        TbBizPlaceEmployeeExample.Criteria criterial = example.createCriteria();
        if (cityId != null){
            criterial.andCityIdEqualTo(cityId);
        }
        if (StringUtils.isNotEmpty(keyWords)) {
            criterial.andNameLike("%" + keyWords + "%");
        }
        example.setPage(new Page(pageNum * pageSize, pageSize, totalNum));
        List<TbBizPlaceEmployee> employeeList = tbBizPlaceEmployeeMapper.selectByExample(example);
        return  employeeList;
    }

    /**
     * 根据id获取商务信息
     * @param id
     * @return
     */
    @Override
    public TbBizPlaceEmployee getEmployeeById(Integer id) {
        return tbBizPlaceEmployeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public APIResponse updateEmployeeById(EmployeeFormDataVo employeeFormDataVo) {
        TbBizPlaceEmployee pEmployee = new TbBizPlaceEmployee();
        pEmployee.setId(employeeFormDataVo.getId());
        pEmployee.setName(employeeFormDataVo.getName());
        pEmployee.setCityId(employeeFormDataVo.getCityId());
        pEmployee.setMobile(employeeFormDataVo.getMobile());
        pEmployee.setParentId(employeeFormDataVo.getParentId());
        pEmployee.setBasicSalary(employeeFormDataVo.getBasic_salary());
        pEmployee.setUid(employeeFormDataVo.getUserId());
        pEmployee.setEntryTime(employeeFormDataVo.getEntryTime());
        pEmployee.setTitle(employeeFormDataVo.getTitle());
        tbBizPlaceEmployeeMapper.updateByPrimaryKey(pEmployee);
        return APIResponse.returnSuccess();
    }

    @Override
    public APIResponse deleteEmployeeById(Integer id) {
        tbBizPlaceEmployeeMapper.deleteByPrimaryKey(id);
        return APIResponse.returnSuccess();
    }



    /*
    获得当前登录用户的职称
     1 商务总经理，2，区域经理，3城市经理，4商务主管，5，商务代表
     */

    @Override
    public Integer getEmployeeTitleByUid(Integer uid) {
        TbBizPlaceEmployeeExample example = new TbBizPlaceEmployeeExample();
        TbBizPlaceEmployeeExample.Criteria criteria = example.createCriteria();
        criteria.andUidEqualTo(uid);//查询出下一级的成员
        List<TbBizPlaceEmployee> list = tbBizPlaceEmployeeMapper.selectByExample(example);
        if (list != null && list.size()>0){
            TbBizPlaceEmployee vo=  list.get(0);
            return vo.getTitle();
        }
        return null;
    }

    /*
 获得当前职称
  */
    public TbBizPlaceEmployee getTitleRoleByUid(VisitedFromDataVo vo) {
        TbBizPlaceEmployeeExample example = new TbBizPlaceEmployeeExample();
        TbBizPlaceEmployeeExample.Criteria criteria = example.createCriteria();
        criteria.andUidEqualTo(vo.getUid());
        List<TbBizPlaceEmployee> list = tbBizPlaceEmployeeMapper.selectByExample(example);
        if (null != list && list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }

}
